<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
        <el-form ref="form" :model="form" :rules="rules">
            <el-form-item label="升级方式（订单完成后、付款后）" :label-width="formLabelWidth" prop="upMode">
                <fortress-radio-group
                        v-model="form.upMode"
                        :dictList="MemberUpModeList"
                >
                </fortress-radio-group>
            </el-form-item>
            <el-form-item label="是否开启账户充值" :label-width="formLabelWidth" prop="rechargeStatus">
                <fortress-radio-group
                        v-model="form.rechargeStatus"
                        :dictList="YesNoList"
                >
                </fortress-radio-group>
            </el-form-item>
            <el-form-item label="是否开启余额提现" :label-width="formLabelWidth" prop="withdrawalStatus">
                <fortress-radio-group
                        v-model="form.withdrawalStatus"
                        :dictList="YesNoList"
                >
                </fortress-radio-group>
            </el-form-item>
            <el-form-item label="余额提现方式" :label-width="formLabelWidth" prop="withdrawalMethod">
                <fortress-select
                        v-model="form.withdrawalMethod"
                        :dictList="withdrawList"
                        placeholder="请选择余额提现方式"
                        clearable
                ></fortress-select>
            </el-form-item>
            <el-form-item label="最小提现金额（余额满多少才能提现,空或0不限制）" :label-width="formLabelWidth" prop="withdrawalMin">
                <el-input
                        v-model="form.withdrawalMin"
                        autocomplete="off"
                        placeholder="请输入最小提现金额（余额满多少才能提现,空或0不限制）"
                ></el-input>
            </el-form-item>
            <el-form-item label="余额提现手续费(%)" :label-width="formLabelWidth" prop="withdrawalServiceCharge">
                <el-input
                        v-model="form.withdrawalServiceCharge"
                        autocomplete="off"
                        placeholder="请输入余额提现手续费(%)"
                ></el-input>
            </el-form-item>
            <el-form-item label="免手续费金额 开始金额" :label-width="formLabelWidth" prop="withdrawalFreeStart">
                <el-input
                        v-model="form.withdrawalFreeStart"
                        autocomplete="off"
                        placeholder="请输入免手续费金额 开始金额"
                ></el-input>
            </el-form-item>
            <el-form-item label="免手续费金额 结束金额" :label-width="formLabelWidth" prop="withdrawalFreeEnd">
                <el-input
                        v-model="form.withdrawalFreeEnd"
                        autocomplete="off"
                        placeholder="请输入免手续费金额 结束金额"
                ></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" >
            <el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
            <el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
    import { membersetingInfo, membersetingSave, membersetingEdit } from "@/api/member/memberseting.js";
    import { dictList,enumList, } from "@/api/public.api.js";
    import * as ftVal from "@/common/fortressValidator.js";
    export default {
        data() {
            return {
                visible: false,
                form: {
                    upMode:"",
                    rechargeStatus:"",
                    withdrawalStatus:"",
                    withdrawalMethod:"",
                    withdrawalMin:"",
                    withdrawalServiceCharge:"",
                    withdrawalFreeStart:"",
                    withdrawalFreeEnd:"",
                },
                formLabelWidth: "120px",
                rules: {
                    withdrawalMin:[
                          { validator: ftVal.validatePInteger,trigger: "blur"},
                      ],
                    withdrawalServiceCharge:[
                          { validator: ftVal.validateTwoPoint,trigger: "blur"},
                      ],
                    withdrawalFreeStart:[
                          { validator: ftVal.validatePInteger,trigger: "blur"},
                      ],
                    withdrawalFreeEnd:[
                          { validator: ftVal.validatePInteger,trigger: "blur"},
                      ],
                },
                withdrawList:[],
                MemberUpModeList:[],
                YesNoList:[],
            };
        },
        created() {
        },
        methods: {
            queryDictList(){
                this.withdrawList = [];
                dictList("withdraw").then((resp) => {
                  if (resp.code == 0) {
                    this.withdrawList = resp.data;
                  }
                });
            },
            queryEnumList(){
                this.MemberUpModeList = [];
                enumList("MemberUpMode","").then((resp) => {
                  if (resp.code == 0) {
                    this.MemberUpModeList = resp.data;
                  }
                });
                this.YesNoList = [];
                enumList("YesNo","").then((resp) => {
                  if (resp.code == 0) {
                    this.YesNoList = resp.data;
                  }
                });
            },
            init(id) {
                this.resetForm();
                this.queryDictList();
                this.queryEnumList();
                //修改
                if (id) {
                    membersetingInfo(id).then((resp) => {
                        if (resp.code == 0) {
                            this.$nextTick(() => {
                                this.form = {
                                    ...this.form,
                                    ...resp.data,
                                };
                            });
                        } else {
                            this.$msg({
                                message: resp.msg,
                                type: "error",
                            });
                        }
                    });
                }
                this.visible = true;
            },
            //提交表单数据
            submitForm() {
                this.$refs["form"].validate((valid) => {
                if (valid) {
                    //修改
                    if (this.form.id) {
                        membersetingEdit(this.form).then((resp) => {
                            if (resp.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "操作成功!",
                                });
                            this.$emit("refreshData");
                            this.visible = false;
                            } else {
                                this.$msg({
                                    message: resp.msg,
                                    type: "error",
                                });
                            }
                        });
                    //新增
                    } else {
                        membersetingSave(this.form).then((resp) => {
                            if (resp.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "操作成功!",
                                });
                                this.$emit("refreshData");
                                this.visible = false;
                            } else {
                                this.$msg({
                                    message: resp.msg,
                                    type: "error",
                                });
                            }
                        });
                    }
                } else {
                    console.log("error submit!!");
                    return false;
                }
                });
            },
            resetForm() {
                this.form = {
                    upMode:"",
                    rechargeStatus:"",
                    withdrawalStatus:"",
                    withdrawalMethod:"",
                    withdrawalMin:"",
                    withdrawalServiceCharge:"",
                    withdrawalFreeStart:"",
                    withdrawalFreeEnd:"",
                };
                this.$nextTick(() => {
                    if (this.$refs["form"]) {
                        this.$refs["form"].resetFields();
                    }
                });
            },
        },
    };
</script>
<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    .el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
        height: auto;
        max-height: 274px;
        padding: 7px;
        overflow: hidden;
        overflow-y: auto;
        font-weight: normal;
    }
</style>
